/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Table 1 - Non-Union Wages and Outside Options, and Appendix Table 4
  Notes     : This file has options for:
			: demographic group `1', 
            : type of transitions `2'
			: selection correction `3'
			: See main text for details
*****************************************************************************************/


clear
set more off

// Table 1 & Table 4 (Appendix)


// data
use ${wd}/reg_dataRRv3_${ind}_${area}_`1'_case_`2'.dta , clear

// Selection controls
if "`3'" == "propic" {
	foreach x of num 1/4{
		g dpropic_`x' = dpropic^`x'
		}
	global selectcnt dpropic_*
	global selecttest dpropic_1=dpropic_2=dpropic_3=dpropic_4=0
}

if "`3'" == "cert" {
	gen dterm1 = dcert1_c
	gen dterm2v1 = dcert2_c
	gen interact1 = dterm1 * dterm2

	foreach x of num 1/2{
		g dt1_`x' = dterm1^`x'
		g dt2_`x' = dterm2v1^`x'
		g in_`x' = 	interact1^`x'
		}
	global selectcnt dt1_* dt2_* in_*
	global selecttest dt1_1=dt1_2=dt2_1=dt2_2=in_1=in_2=0
}

// First stage reporting
* - F STAT
if "$FSTAT" == "F" local FPOS = 8
if "$FSTAT" == "F" local fs   "-Stats."
* - p Value
if "$FSTAT" == "p" local FPOS = 11
if "$FSTAT" == "p" local fs   "-values"


// Table parameters

	// Outside options
	global rents   dPE_n dPE_u  
	global rents2  dPE_0n dPE_1n dPE_u  
	
	// IVs
	global IV1 IV_n IV_u
	global IV2  IV_1n IV_0n IV_u

// absorbed interactions
global inter c.LP#controls
global inter2 c.LP#(${area}#year)

// Table 1
// 1
ivreghdfe dwage  dER   $selectcnt  ( $rents =   $IV1 )  [aw=weight], a(controls ${inter} ${inter2}  )  cluster($clustlevel) first  
eststo a

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 2)

estadd local YxI   "Yes"
estadd local PxIxY "Yes"
estadd local PxCxY "Yes"
estadd local pic   "Yes"


// 2
ivreghdfe dwage     $selectcnt   ( $rents =   $IV1 )  [aw=weight],a(controls ${inter} ${inter2} )  cluster($clustlevel) first  
eststo b

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 2)

estadd local YxI   "Yes"
estadd local PxIxY "Yes"
estadd local PxCxY "Yes"
estadd local pic   "Yes"

// Not shown in paper

ivreghdfe dwage  dER  $selectcnt  ( $rents =   $IV2 )  [aw=weight],a(controls  ${inter} ${inter2}  )  cluster($clustlevel)  first
eststo c

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 2)

estadd local YxI   "Yes"
estadd local PxIxY "Yes"
estadd local PxCxY "Yes"
estadd local pic   "Yes"

// over id test
scalar hj = e(jp)


// 3
ivreghdfe dwage  dER $selectcnt   ( $rents2 = $IV2 )  [aw=weight],a(controls  ${inter} ${inter2} )  cluster($clustlevel)  first
eststo d

estadd scalar SW${FSTAT}0n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}1n 	= el(e(first),`FPOS', 2)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 3)

estadd local YxI   "Yes"
estadd local PxIxY "Yes"
estadd local PxCxY "Yes"
estadd local pic   "Yes"
estadd scalar OverID = hj


// 4
ivreghdfe dwage  dER    ( $rents =   $IV1 )  [aw=weight],a(controls ${inter} ${inter2}  )  cluster($clustlevel)  first
eststo e

estadd scalar SW${FSTAT}n 	= el(e(first),`FPOS', 1)
estadd scalar SW${FSTAT}u 	= el(e(first),`FPOS', 2)

estadd local YxI   "Yes"
estadd local PxIxY "Yes"
estadd local PxCxY "Yes"
estadd local pic   ""


// Union regressions

preserve


local 1 All
use ${wd}/reg_dataRRv3_Union_${ind}_${area}_`1'_case_0.dta, clear

keep if $ind < 47

// Selection controls
if "`3'" == "propic" {
	foreach x of num 1/4{
		g dpropic_`x' = dpropic^`x'
		}
	global selectcnt dpropic_*
	global selecttest dpropic_1=dpropic_2=dpropic_3=dpropic_4=0
}

if "`3'" == "cert" {
	gen dterm1 = dcert1_c
	gen dterm2v1 = dcert2_c
	gen interact1 = dterm1 * dterm2

	foreach x of num 1/2{
		g dt1_`x' = dterm1^`x'
		g dt2_`x' = dterm2v1^`x'
		g in_`x' = 	interact1^`x'
		}
	global selectcnt dt1_* dt2_* in_*
	global selecttest dt1_1=dt1_2=dt2_1=dt2_2=in_1=in_2=0
}

drop if year == 5

ivreghdfe dwage dER  $selectcnt  (dE_c  =  IV_n IV_u      ) [aw=weight]  ,a( controls )  cluster($clustlevel) first       
eststo unionspec1

estadd scalar SW${FSTAT}7 	= el(e(first),`FPOS',1)
estadd local YxI "Yes"
estadd local pic   "Yes"


ivreghdfe dwage dER    (dE_c  =    IV_n IV_u         ) [aw=weight]  ,a( controls )  cluster($clustlevel) first       
eststo unionspec2

estadd scalar SW${FSTAT}7 	= el(e(first),`FPOS',1)
estadd local YxI "Yes"
estadd local pic   ""


restore

lab var dE_c  "$ \Delta E_{uict} $"

esttab a b c d e  unionspec1 unionspec2 , se  star(* 0.10 ** 0.05 *** 0.01) ///
		scalar(  ///
		"space2  \midrule First-Stage: " ///
		"SWF1  \ \ $ F $-Stat." "SWp1 \ \ \textit{p}-val" ///
		"SWF2  \ \ $ F $-Stat." "SWp2 \ \ \textit{p}-val" ///
		"OverID \ \ Over-id. \textit{p}-val")	


// Table notes 
local notes This table reports 2SLS estimates based on specification \eqref{wagefull}. ///
	Columns (1)–(4) use the decadal change in the regression-adjusted average weekly wage of non-union workers in an industry–city cell as the dependent variable; columns (5)–(6) use the corresponding wage change for union workers. Wage data are constructed from CPS microdata from 1980–2010 across 50 industries and 93 cities. Standard errors, in parentheses, are clustered at the $clustlbl ///
	level. Reported first-stage $FSTAT`fs' are from Sanderson–Windmeijer tests for instrument relevance; the overidentification $ p $-value is from the  Hansen $ J $ test. 
 
if "`3'" == "propic" {
	
		local title="Non-Union Wages and Outside Options \label{tab:4:`1'}"	 
		esttab  a b  d e    unionspec1 unionspec2 using "${tables}//Table_01.tex", replace  ///
			booktabs nomtitles ///
			mgroups( "Non-Union"  "Union" , pattern(1 0 0 0  1 0 ) /// 
			prefix(\multicolumn{@span}{c}{) suffix(})  ///
			span erepeat(\cmidrule(lr){@span})) ///
			b(a2) se(a2) se  sfmt(3) star(* 0.10 ** 0.05 *** 0.01) label  ///
			keep(dPE_n dPE_0n dPE_1n dPE_u dE_c dER ) nonotes  tabnote(`notes')  ///
			title("`title'" ) ///
			order(dPE_n dPE_0n dPE_1n dPE_u dE_c dER  ) ///
			scalar( /// 
			"YxI Year $\times$ Ind."  ///
			"PxIxY $  P^{ne}_{ict-1}\times $ Ind. $ \times $ Year" ///
			"PxCxY $  P^{ne}_{ict-1}\times $ City $ \times $ Year" ///
			"select \midrule Selection controls" ///
			"pic \ \ \ $ \Delta P_{ict}$ Quadratic" ///
			"space2  \midrule First-Stage $ ${FSTAT} $`fs': " ///
			"SW${FSTAT}n  \ \ \ \ $ \Delta\left((1-\PUN) \cdot E_{nict} \right) $" ///
			"SW${FSTAT}u  \ \ \ \ $ \Delta\left(\PUN \cdot E_{uict} \right) $" ///
			"SW${FSTAT}0n  \ \  \ \ $ \Delta\left((1-\PUN) \cdot E_{uct|n i} \right) $" ///
			"SW${FSTAT}1n  \ \ \ \ $ \Delta\left((1-\PUN)  \cdot E_{nct|n i} \right) $" ///
			"SW${FSTAT}7  \ \ \ \ $  \Delta E_{uict}  $" ///
			 "OverID  Over-id. \textit{p}-value:")
	

}

if "`3'" == "cert" {
	
		local title="Non-Union Wages and Outside Options \label{tab:4:alt:`1'}"	 
		esttab   a b d e    unionspec1 unionspec2  using "${tables}//Table_01_alt_select.tex", replace  ///
			booktabs nomtitles ///
			mgroups( "Non-Union"  "Union" , pattern(1 0 0 0 1 0 ) /// 
			prefix(\multicolumn{@span}{c}{) suffix(})  ///
			span erepeat(\cmidrule(lr){@span})) ///
			b(a2) se(a2) se  sfmt(3) star(* 0.10 ** 0.05 *** 0.01) label  ///
			keep(dPE_n dPE_0n dPE_1n dPE_u dE_c dER   ) nonotes  tabnote(`notes')  ///
			title("`title'" ) ///
			order(dPE_n dPE_0n dPE_1n dPE_u dE_c dER  ) ///
			scalar( /// 
			"YxI Year $\times$ Ind."  ///
			"PxIxY $  P^{ne}_{ict-1}\times $ Ind. $ \times $ Year" ///
			"PxCxY $  P^{ne}_{ict-1}\times $ City $ \times $ Year" ///
			"select \midrule Selection controls" ///
			"pic \ \ \ Election Vars." ///
			"space2  \midrule First-Stage $ ${FSTAT} $`fs': " ///
			"SW${FSTAT}n  \ \ \ \ $ \Delta\left((1-\PUN) \cdot E_{nict} \right) $" ///
			"SW${FSTAT}u  \ \ \ \ $ \Delta\left(\PUN \cdot E_{uict} \right) $" ///
			"SW${FSTAT}0n  \ \  \ \ $ \Delta\left((1-\PUN) \cdot E_{uct|n i} \right) $" ///
			"SW${FSTAT}1n  \ \ \ \ $ \Delta\left((1-\PUN)  \cdot E_{nct|n i} \right) $" ///
			"SW${FSTAT}7  \ \ \ \ $  \Delta E_{uict}  $" ///
			"OverID  Over-id. \textit{p}-value:")
			
}

